<template>
  <div class="projectAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div class="search-button" v-if="!contactAddShow && !saleAddShow">
      <el-button
        v-if="pageType === 'check' && route.name === 'WorkCheck'"
        class="btn save"
        @click="handleCheckBtn(projectFormRef, 3, '同意')"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >同意</el-button
      >
      <el-button
        v-if="pageType === 'check' && route.name === 'WorkCheck'"
        class="btn withdraw"
        @click="handleCheckBtn(projectFormRef, 4, '拒绝')"
        ><el-icon class="icon" color="#fff"> <DocumentDelete /> </el-icon
        >拒绝</el-button
      >

      <el-button
        v-if="pageType === 'check' && route.name === 'WorkCheck'"
        class="btn withdraw"
        @click="handleCheckBtn(projectFormRef, 6, '驳回')"
        ><el-icon class="icon" color="#fff"> <DocumentDelete /> </el-icon
        >驳回</el-button
      >
      <el-button
        v-if="pageType !== 'detail' && pageType !== 'check'"
        class="btn submit"
        @click="handleAddSave(projectFormRef, 1)"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >提交</el-button
      >
      <!-- <el-button
        v-if="pageType == 'edit'"
        class="btn withdraw"
        @click="handleCheckBtn(projectFormRef, 5, '撤回')"
        ><el-icon class="icon" color="#fff"> <DocumentDelete /> </el-icon
        >撤回</el-button
      > -->
      <el-button
        v-if="pageType !== 'detail' && route.name === 'WorkApply'"
        class="btn save"
        @click="handleAddSave(projectFormRef, 0)"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >存为草稿</el-button
      >
      <el-button class="btn direction" @click="handleBack"
        ><el-icon class="icon" color="#fff"> <Back /> </el-icon
        >返回</el-button
      >
    </div>
    <el-tabs type="border-card" v-if="pageType === 'detail'">
      <el-tab-pane label="工单信息">
        <el-collapse v-model="activeNames">
          <!-- 基础信息 -->
          <el-collapse-item
            name="1"
            v-if="!contactAddShow && !saleAddShow"
            class="transform-down"
          >
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title"
                >{{
                  pageType === "detail"
                    ? ""
                    : pageType === "add"
                    ? "新增"
                    : pageType === "edit"
                    ? "编辑"
                    : "审核"
                }}工单信息</span
              >
            </template>
            <div class="project-info">
              <el-form
                :model="projectForm"
                ref="projectFormRef"
                :rules="queryRules"
                label-width="120px"
                v-enterToNext
                :disabled="pageType === 'detail' || pageType === 'check'"
              >
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="申请人：">
                      <el-input
                        v-model="projectForm.applicantName"
                        placeholder="自动获取"
                        clearable
                        class="search-input"
                        disabled
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="申请部门：" label-width="120">
                      <el-input
                        v-model="projectForm.deptName"
                        placeholder="自动获取"
                        clearable
                        class="search-input"
                        disabled
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="创建时间：">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="projectForm.createTime"
                        class="search-input"
                        type="datetime"
                        placeholder="自动获取"
                        disabled
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item
                      label="工单名称："
                      prop="workOrderName"
                      label-width="120"
                    >
                      <el-input
                        v-model="projectForm.workOrderName"
                        placeholder="请输入"
                        clearable
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="业务人员：" prop="userId">
                      <el-select
                        v-model="projectForm.userId"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                      >
                        <el-option
                          v-for="dict in userList"
                          :key="dict.userId"
                          :label="dict.nickName"
                          :value="dict.userId"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="7">
                    <el-form-item
                      label-width="120"
                      label="项目地址："
                      prop="city"
                    >
                      <el-select
                        filterable
                        style="width: 93px"
                        v-model="projectForm.province"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                        @change="handleProvince('province')"
                      >
                        <el-option
                          v-for="dict in provinceList"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                      <el-select
                        filterable
                        style="width: 93px; margin: 0 5px"
                        v-model="projectForm.city"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                        @change="handleProvince('city')"
                      >
                        <el-option
                          v-for="dict in cityList"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                      <el-select
                        filterable
                        style="width: 93px"
                        v-model="projectForm.area"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                      >
                        <el-option
                          v-for="dict in areaList"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="11">
                    <el-form-item
                      label="详细地址："
                      prop="address"
                      label-width="100"
                    >
                      <el-input
                        style="width: 100%"
                        v-model="projectForm.address"
                        placeholder="请输入"
                        clearable
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="0">
                  <el-col :span="6">
                    <el-form-item
                      label="施工完结时间："
                      prop="finishTime"
                      label-width="120"
                    >
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="projectForm.finishTime"
                        class="search-input"
                        type="datetime"
                        placeholder="请选择"
                      />
                    </el-form-item>
                  </el-col>

                  <el-col :span="6">
                    <el-form-item label="是否有项目：" prop="isProject">
                      <el-select
                        v-model="projectForm.isProject"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                      >
                        <el-option
                          v-for="dict in flagList"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="6" v-if="projectForm.isProject === 1">
                    <el-form-item
                      label="项目名称："
                      prop="projectName"
                      label-width="120"
                    >
                      <el-select
                        v-model="projectForm.projectId"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                        @change="handleProjectChange"
                      >
                        <el-option
                          v-for="dict in projectList"
                          :key="dict.projectId"
                          :label="dict.projectName"
                          :value="dict.projectId"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="6" v-if="projectForm.isProject === 1">
                    <el-form-item label="项目编号：">
                      <el-input
                        v-model="projectForm.projectCode"
                        placeholder="回显项目编号"
                        clearable
                        class="search-input"
                        disabled
                      >
                      </el-input>
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row>
                  <el-col :span="6">
                    <el-form-item
                      label="工单类型："
                      prop="workOrderType"
                      label-width="120"
                    >
                      <el-select
                        v-model="projectForm.workOrderType"
                        placeholder="请选择"
                        clearable
                        class="search-input"
                      >
                        <el-option
                          v-for="dict in typeList"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col :span="6">
                    <el-form-item label="期望开始时间：" prop="desireStartTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="projectForm.desireStartTime"
                        class="search-input"
                        type="datetime"
                        placeholder="请选择"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="期望结束时间：" prop="desireEndTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="projectForm.desireEndTime"
                        class="search-input"
                        type="datetime"
                        placeholder="请选择"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="工单描述：" prop="remark">
                      <el-input
                        style="width: 100%"
                        v-model="projectForm.remark"
                        type="textarea"
                        maxlength="2000"
                        placeholder="请输入"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </el-collapse-item>
          <!-- 添加施工联系人 -->
          <el-collapse-item
            name="2"
            style="position: relative"
            v-if="saleAddShow && !contactAddShow"
            class="transform-up"
          >
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">添加施工联系人</span>
            </template>
            <div class="project-info">
              <el-form
                :model="saleForm"
                ref="saleFormRef"
                :rules="querySaleRules"
                label-width="100px"
                v-enterToNext
              >
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="联系人：" prop="contactsName">
                      <el-input
                        v-model="saleForm.contactsName"
                        placeholder="请输入"
                        clearable
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="联系方式：" prop="contactsPhone">
                      <el-input
                        v-model="saleForm.contactsPhone"
                        placeholder="请输入"
                        clearable
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="备注：" prop="remark">
                      <el-input
                      maxlength="2000"
                        style="width: 96.2%"
                        v-model="saleForm.remark"
                        placeholder="请输入"
                        clearable
                        class="search-input"
                        @keyup.enter.stop="handleSaleAdd(saleFormRef)"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </el-collapse-item>
          <!-- 施工联系人明细 -->
          <el-collapse-item
            name="3"
            style="position: relative"
            v-show="!contactAddShow"
            class="transform-down"
          >
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">施工联系人</span>
              <div
                v-if="
                  pageType !== 'detail' && pageType !== 'check' && !saleAddShow
                "
                class="search-button button-position"
              >
                <el-button class="btn add" @click.stop="handleSaleOperate"
                  ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
                  >添加</el-button
                >
              </div>
              <div
                class="search-button button-position"
                v-else
                v-if="pageType !== 'detail' && pageType !== 'check'"
              >
                <el-button
                  class="btn add"
                  @click.stop="handleSaleAdd(saleFormRef)"
                  ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
                  >添加</el-button
                >
                <el-button class="btn direction" @click.stop="handleSaleBack"
                  ><el-icon class="icon" color="#fff"> <ArrowUp /> </el-icon
                  >关闭</el-button
                >
              </div>
            </template>
            <div class="contact-info">
              <!-- 表格数据 -->
              <el-table
                :data="projectForm.mainContactsInfoRespVoList"
                border
                :height="
                  pageType === 'detail'
                    ? '100%'
                    : contactAddShow
                    ? '490px'
                    : '280px'
                "
                :header-cell-style="{ 'text-align': 'center' }"
              >
                <!-- 操作模式 -->
                <el-table-column
                  label="操作"
                  align="center"
                  :show-overflow-tooltip="true"
                  width="100px"
                  v-if="pageType !== 'detail' && pageType !== 'check'"
                >
                  <!-- v-if="!operateType" -->
                  <template #default="{ row, $index }">
                    <div class="table-operate">
                      <img
                        src="@/assets/icons/images/edit.png"
                        class="table-icon"
                        title="编辑"
                        @click="handleOperateSec('edit', row, $index)"
                      />
                      <img
                        src="@/assets/icons/images/delete.png"
                        class="table-icon"
                        title="删除"
                        @click="handleOperateSec('delete', row, $index)"
                      />
                    </div> </template
                ></el-table-column>
                <el-table-column
                  label="联系人"
                  prop="contactsName"
                  :show-overflow-tooltip="true"
                  min-width="160"
                />
                <el-table-column
                  label="联系方式"
                  prop="contactsPhone"
                  :show-overflow-tooltip="true"
                  min-width="160"
                />
                <el-table-column
                  label="备注"
                  prop="remark"
                  :show-overflow-tooltip="true"
                  min-width="160"
                />
              </el-table>
            </div>
          </el-collapse-item>
        </el-collapse>
      </el-tab-pane>

      <el-tab-pane label="审批进度"> 待开发 </el-tab-pane>
      <el-tab-pane label="实施进度">
        <el-collapse v-model="activeNames">
          <!-- 基础信息 -->
          <el-collapse-item name="1" class="transform-down">
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">分配信息</span>
            </template>
            <div class="project-info">
              <el-form
                :model="impleForm"
                ref="followFormRef"
                :rules="queryRules"
                label-width="120px"
                v-enterToNext
                :disabled="pageType === 'detail'"
              >
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="任务名称：" prop="deptId">
                      <el-input
                        disabled
                        v-model="impleForm.maintanceTaskName"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="分配人：" prop="allotRelList">
                      <el-input
                        disabled
                        v-model="impleForm.executors"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="分配时间：" prop="allotRelList">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.createTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="计划开始时间：" prop="startTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.startTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="计划结束时间：" prop="endTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.endTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="备注：" >
                      <el-input
                      maxlength="2000"
                        style="width: 100%"
                        v-model="impleForm.remark"
                        type="textarea"
                        placeholder=""
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </el-collapse-item>
          <!-- 跟进记录 -->
          <el-collapse-item name="2" class="transform-down">
            <template #title>
              <span class="circle"></span>
              <span class="collapse-title">施工信息</span>
            </template>
            <div class="project-info">
              <el-form
                :model="impleForm"
                ref="followFormRef"
                :rules="queryRules"
                label-width="120px"
                v-enterToNext
                :disabled="pageType === 'detail'"
              >
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="工程师：" prop="deptId">
                      <el-input
                        disabled
                        v-model="impleForm.executors"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="分配时间：" prop="allotRelList">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.createTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="实际开始时间：" prop="startTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.actualStartTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="实际结束时间：" prop="endTime">
                      <el-date-picker
                        value-format="YYYY-MM-DD HH:mm:ss"
                        v-model="impleForm.finishTime"
                        class="search-input"
                        type="datetime"
                        placeholder=""
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row v-if="impleForm.suppFileInfoVos.length">
                  <el-col :span="6">
                    <el-form-item label="交付物：">
                      <Upload
                        :fileList="impleForm.suppFileInfoVos"
                        :limit="1"
                        limitation="templatelimitation"
                        accept=".doc,.docx"
                        :isFull="true"
                        :isCanDelete="false"
                        :showButton="false"
                        :disabled="true"
                        @success="(result) => uploadSuccess(result)"
                        @deleteFile="(result) => uploadRemove(result)"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item label="故障类型：">
                      <el-input
                        disabled
                        v-model="impleForm.faultTypeName"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="反馈情况：">
                      <el-input
                        disabled
                        v-model="impleForm.feedbackName"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                  <el-col :span="6">
                    <el-form-item label="申请物料：">
                      <el-input
                        disabled
                        v-model="impleForm.erpDeliveryApplies"
                        placeholder=""
                        clearable
                        style="width: 100%"
                        class="search-input"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="工单关结描述：">
                      <el-input
                        style="width: 100%"
                        v-model="impleForm.workDesc"
                        type="textarea"
                        maxlength="2000"
                        placeholder="请输入"
                      ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </el-collapse-item>
        </el-collapse>
      </el-tab-pane>
    </el-tabs>
    <el-collapse v-model="activeNames" v-else>
      <!-- 基础信息 -->
      <el-collapse-item
        name="1"
        v-if="!contactAddShow && !saleAddShow"
        class="transform-down"
      >
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title"
            >{{
              pageType === "detail"
                ? ""
                : pageType === "add"
                ? "新增"
                : pageType === "edit"
                ? "编辑"
                : "审核"
            }}工单信息</span
          >
        </template>
        <div class="project-info">
          <el-form
            :model="projectForm"
            ref="projectFormRef"
            :rules="queryRules"
            label-width="120px"
            v-enterToNext
            :disabled="pageType === 'detail' || pageType === 'check'"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item label="申请人：">
                  <el-input
                    v-model="projectForm.applicantName"
                    placeholder="自动获取"
                    clearable
                    class="search-input"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="申请部门：" label-width="120">
                  <el-input
                    v-model="projectForm.deptName"
                    placeholder="自动获取"
                    clearable
                    class="search-input"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间：">
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="projectForm.createTime"
                    class="search-input"
                    type="datetime"
                    placeholder="自动获取"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="工单名称："
                  prop="workOrderName"
                  label-width="120"
                >
                  <el-input
                    v-model="projectForm.workOrderName"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="业务人员：" prop="userId">
                  <el-select
                    v-model="projectForm.userId"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in userList"
                      :key="dict.userId"
                      :label="dict.nickName"
                      :value="dict.userId"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label-width="100" label="项目地址：" prop="city">
                  <el-select
                    filterable
                    style="width: 93px"
                    v-model="projectForm.province"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                    @change="handleProvince('province')"
                  >
                    <el-option
                      v-for="dict in provinceList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                  <el-select
                    filterable
                    style="width: 93px; margin: 0 5px"
                    v-model="projectForm.city"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                    @change="handleProvince('city')"
                  >
                    <el-option
                      v-for="dict in cityList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                  <el-select
                    filterable
                    style="width: 93px"
                    v-model="projectForm.area"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in areaList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="详细地址："
                  prop="address"
                  label-width="100"
                >
                  <el-input
                    style="width: 100%"
                    v-model="projectForm.address"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="0">
              <el-col :span="6">
                <el-form-item
                  label="施工完结时间："
                  prop="finishTime"
                  label-width="120"
                >
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="projectForm.finishTime"
                    class="search-input"
                    type="datetime"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label="是否有项目：" prop="isProject">
                  <el-select
                    v-model="projectForm.isProject"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in flagList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6" v-if="projectForm.isProject === 1">
                <el-form-item
                  label="项目名称："
                  prop="projectName"
                  label-width="120"
                >
                  <el-select
                    v-model="projectForm.projectId"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                    @change="handleProjectChange"
                  >
                    <el-option
                      v-for="dict in projectList"
                      :key="dict.projectId"
                      :label="dict.projectName"
                      :value="dict.projectId"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6" v-if="projectForm.isProject === 1">
                <el-form-item label="项目编号：">
                  <el-input
                    v-model="projectForm.projectCode"
                    placeholder="回显项目编号"
                    clearable
                    class="search-input"
                    disabled
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="6">
                <el-form-item
                  label="工单类型："
                  prop="workOrderType"
                  label-width="120"
                >
                  <el-select
                    v-model="projectForm.workOrderType"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in typeList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label="期望开始时间：" prop="desireStartTime">
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="projectForm.desireStartTime"
                    class="search-input"
                    type="datetime"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="期望结束时间：" prop="desireEndTime">
                  <el-date-picker
                    value-format="YYYY-MM-DD HH:mm:ss"
                    v-model="projectForm.desireEndTime"
                    class="search-input"
                    type="datetime"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="工单描述：" prop="remark">
                  <el-input
                    style="width: 100%"
                    v-model="projectForm.remark"
                    type="textarea"
                    maxlength="2000"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
      <!-- 添加施工联系人 -->
      <el-collapse-item
        name="2"
        style="position: relative"
        v-if="saleAddShow && !contactAddShow"
        class="transform-up"
      >
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">添加施工联系人</span>
        </template>
        <div class="project-info">
          <el-form
            :model="saleForm"
            ref="saleFormRef"
            :rules="querySaleRules"
            label-width="100px"
            v-enterToNext
          >
            <el-row>
              <el-col :span="6">
                <el-form-item label="联系人：" prop="contactsName">
                  <el-input
                    v-model="saleForm.contactsName"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="联系方式：" prop="contactsPhone">
                  <el-input
                    v-model="saleForm.contactsPhone"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="备注：" prop="remark">
                  <el-input
                  maxlength="2000"
                    style="width: 96.2%"
                    v-model="saleForm.remark"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                    @keyup.enter.stop="handleSaleAdd(saleFormRef)"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
      <!-- 施工联系人明细 -->
      <el-collapse-item
        name="3"
        style="position: relative"
        v-show="!contactAddShow"
        class="transform-down"
      >
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">施工联系人</span>
          <div
            v-if="pageType !== 'detail' && pageType !== 'check' && !saleAddShow"
            class="search-button button-position"
          >
            <el-button class="btn add" @click.stop="handleSaleOperate"
              ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
              >添加</el-button
            >
          </div>
          <div
            class="search-button button-position"
            v-else
            v-if="pageType !== 'detail' && pageType !== 'check'"
          >
            <el-button class="btn add" @click.stop="handleSaleAdd(saleFormRef)"
              ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
              >添加</el-button
            >
            <el-button class="btn direction" @click.stop="handleSaleBack"
              ><el-icon class="icon" color="#fff"> <ArrowUp /> </el-icon
              >关闭</el-button
            >
          </div>
        </template>
        <div class="contact-info">
          <!-- 表格数据 -->
          <el-table
            :data="projectForm.mainContactsInfoRespVoList"
            border
            :height="
              pageType === 'detail'
                ? '100%'
                : contactAddShow
                ? '490px'
                : '280px'
            "
            :header-cell-style="{ 'text-align': 'center' }"
          >
            <!-- 操作模式 -->
            <el-table-column
              label="操作"
              align="center"
              :show-overflow-tooltip="true"
              width="100px"
              v-if="pageType !== 'detail' && pageType !== 'check'"
            >
              <!-- v-if="!operateType" -->
              <template #default="{ row, $index }">
                <div class="table-operate">
                  <img
                    src="@/assets/icons/images/edit.png"
                    class="table-icon"
                    title="编辑"
                    @click="handleOperateSec('edit', row, $index)"
                  />
                  <img
                    src="@/assets/icons/images/delete.png"
                    class="table-icon"
                    title="删除"
                    @click="handleOperateSec('delete', row, $index)"
                  />
                </div> </template
            ></el-table-column>
            <el-table-column
              label="联系人"
              prop="contactsName"
              :show-overflow-tooltip="true"
              min-width="160"
            />
            <el-table-column
              label="联系方式"
              prop="contactsPhone"
              :show-overflow-tooltip="true"
              min-width="160"
            />
            <el-table-column
              label="备注"
              prop="remark"
              :show-overflow-tooltip="true"
              min-width="160"
            />
          </el-table>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import Upload from "@/components/Upload/index";
import {
  getProvinceApi,
  getCustomerListApi,
  getUserListApi,
} from "@/api/information/customer";
import {
  addProjectApi,
  getProjectApi,
  editProjectApi,
  checkProjectApi,
} from "@/api/sales/project";
import {
  getProjectListApi,
  addWorkApplyApi,
  editWorkApplyApi,
  getWorkApplyDetailApi,
  checkWorkApi,
} from "@/api/operation/work";
import { getMaterialListApi } from "@/api/information/material";
import { getDictsApi } from "@/api/dict";
import { ElMessage, ElMessageBox } from "element-plus";
const route = useRoute();
const emit = defineEmits([
  // 项目新增组件显隐
  "projectAddShow",
  // 更新项目新增组件显隐
  "update:projectAddShow",
  // 刷新父组件表格数据
  "refreshList",
  // 更新跟进组件显隐
  "updateFollowAddShow",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3", "4", "5"]);
// 客户表单引用
const projectFormRef = ref();
// 联系人表单引用
const contactFormRef = ref();
// 销售表单引用
const saleFormRef = ref();
// 联系人操作类型
const operateType = ref("");
// 详情加载效果
const detailInfoShow = ref(false);
// 联系人当前索引(编辑联系人使用)
const tableIndex = ref(null);
// 销售清单当前索引(编辑销售清单使用)
const tableIndexSec = ref(null);
// 输入框引用
const refInput = ref();
// 联系人输入框引用
const contactRefInput = ref();
// 下拉框引用
const refSelect = ref();
// 销售项目类目列表
const saleCategoryList = ref([]);
// 是否签合同列表
const contractList = ref([]);
// 是否中标列表
const successfulList = ref([]);
// 项目状态列表
const statusList = ref([]);
// 项目类型列表
const typeList = ref([]);
// 客户列表
const customerList = ref([]);
// 设备列表
const deviceList = ref([]);
// 新增联系人表单显隐
const contactAddShow = ref(false);
// 新增销售列表表单显隐
const saleAddShow = ref(false);
// 实施表单
const impleForm = ref({ suppFileInfoVos: [] });
//省市区校验规则
const provinceValidate = (rule, value, callback) => {
  const { province, city, area } = projectForm.value;
  if (!province || !city || !area) {
    callback(new Error("请选择省市区"));
  } else {
    callback();
  }
};
// 项目表单模板
const projectFormTemplate = {
  address: "",
  applicantDate: "",
  applicantId: null,
  approvalStatus: null,
  area: null,
  city: null,
  constructionStatus: null,
  desireEndTime: "",
  desireStartTime: "",
  finishTime: "",
  isProject: null,
  mainContactsInfoRespVoList: [],
  projectId: null,
  projectCode: "",
  province: null,
  remark: "",
  type: 0,
  userId: null,
  workOrderName: "",
  workOrderStatus: null,
  workOrderType: null,
};
const data = reactive({
  // 项目表单
  projectForm: JSON.parse(JSON.stringify(projectFormTemplate)),
  // 联系人表单
  contactForm: {
    name: "",
    phone: "",
    practiceId: null,
    projectId: null,
    remarks: "",
  },
  // 销售清单表单
  saleForm: {
    contactsName: "",
    contactsPhone: "",
    orderContactsId: null,
    workOrderId: null,
    remark: "",
  },
  // 项目表单校验规则
  queryRules: {
    workOrderName: [
      { required: true, message: "请输入工单名称", trigger: "blur" },
    ],
    userId: [{ required: true, message: "请选择业务人员", trigger: "change" }],
    city: [
      {
        required: true,
        validator: provinceValidate,
        message: "请选择项目地址",
        trigger: "change",
      },
    ],
    address: [{ required: true, message: "请输入详细地址", trigger: "blur" }],
    finishTime: [
      { required: true, message: "请选择施工完结时间", trigger: "change" },
    ],
    isProject: [
      { required: true, message: "请选择是否有项目", trigger: "change" },
    ],
    projectId: [
      { required: true, message: "请选择项目名称", trigger: "change" },
    ],
    workOrderType: [
      { required: true, message: "请选择工单类型", trigger: "change" },
    ],
    desireStartTime: [
      { required: true, message: "请选择期望开始时间", trigger: "change" },
    ],
    desireEndTime: [
      { required: true, message: "请选择期望结束时间", trigger: "change" },
    ],
    remark: [{ required: true, message: "请输入工单描述", trigger: "blur" }],
  },
  // 销售清单表单校验规则
  querySaleRules: {
    contactsName: [
      { required: true, message: "请输入联系人", trigger: "blur" },
    ],
    contactsPhone: [
      { required: true, message: "请输入联系方式", trigger: "blur" },
    ],
    remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
  },
  // 省列表
  provinceList: [],
  // 市列表
  cityList: [],
  // 区列表
  areaList: [],
  // 用户列表
  userList: [],
  // 项目列表
  projectList: [],
  flagList: [
    {
      label: "是",
      value: 1,
    },
    {
      label: "否",
      value: 0,
    },
  ],
});
const {
  projectForm,
  contactForm,
  saleForm,
  queryRules,
  querySaleRules,
  provinceList,
  cityList,
  areaList,
  userList,
  flagList,
  projectList,
} = toRefs(data);

/**
 * @description: 保存
 * @param {Object} customerFormRef 客户表单引用
 */
const handleAddSave = async (projectFormRef, type) => {
  // if (type && props.pageType === "edit") {
  //   return handleCheckBtn(projectFormRef, 1, "提交");
  // }
  if (!projectFormRef) return;
  if (!projectForm.value.mainContactsInfoRespVoList.length) {
    return ElMessage.warning("请添加施工联系人");
  }
  await projectFormRef.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定保存以上数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          projectForm.value.type = type;
          let res;
          if (!projectForm.value.workOrderId) {
            res = await addWorkApplyApi(projectForm.value);
          } else {
            res = await editWorkApplyApi(projectForm.value);
          }
          if (res.code === 200) {
            emit("update:projectAddShow", false);
            emit("refreshList", false);
            ElMessage.success(res.msg);
            projectForm.value = JSON.parse(JSON.stringify(projectFormTemplate));
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
    }
  });
};
/**
 * @description: 处理销售类目选择
 * @param {Number} value 当前值
 */
const handleSaleCategory = (value) => {
  saleCategoryList.value.forEach((item) => {
    if (item.value === value) {
      saleForm.value.saleCategoryName = item.label;
    }
  });
  const obj = {
    brand: "",
    saleName: "",
    materialId: null,
    materialUnit: null,
    materialUnitName: "",
    platformName: "",
    technologyParameter: "",
    quantity: null,
  };
  saleForm.value = { ...saleForm.value, ...obj };
};

/**
 * @description: 处理审核状态相关按钮
 * @param {String} type  类型
 * @param {String} title 标题
 */
const handleCheckBtn = (projectFormRef, type, title) => {
  ElMessageBox.confirm(`确定${title}以上数据吗？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    confirmButtonClass: "confirmBtnClass",
    cancelButtonClass: "cancelBtnClass",
  })
    .then(async () => {
      const obj = {
        approvalStatus: type,
        projectId: projectForm.value.projectId,
        workOrderId: projectForm.value.workOrderId,
      };
      const res = await checkWorkApi(obj);
      if (res.code === 200) {
        emit("updateFollowAddShow", false);
        emit("update:projectAddShow", false);
        emit("refreshList", false);
        ElMessage.success(res.msg);
        projectForm.value = JSON.parse(JSON.stringify(projectFormTemplate));
      } else {
        ElMessage.error(res.msg);
      }
    })
    .catch(() => {});
};

/**
 * @description: 返回
 */
const handleBack = () => {
  if (props.pageType === "detail" || props.pageType === "check") {
    emit("updateFollowAddShow", false);
    emit("update:projectAddShow", false);
    projectForm.value = JSON.parse(JSON.stringify(projectFormTemplate));
  } else {
    ElMessageBox.confirm("确定返回上一级吗?以上数据未保存将会丢失!", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      confirmButtonClass: "confirmBtnClass",
      cancelButtonClass: "cancelBtnClass",
    })
      .then(async () => {
        emit("update:projectAddShow", false);
        projectForm.value = JSON.parse(JSON.stringify(projectFormTemplate));
      })
      .catch(() => {});
  }
};

/**
 * @description: 文件上传成功处理
 * @param {Object} response 文件信息
 */
function uploadSuccess(response) {
  impleForm.value.suppFileInfoVos.push({ ...response, businessType: 16 });
}

/**
 * @description: 删除文件
 * @param {Number} index 当前文件索引
 */
function uploadRemove(index) {
  impleForm.value.suppFileInfoVos.splice(index, 1);
}

/**
 * @description: 新增、编辑销售清单
 * @param {Object} saleFormRef 联售清单引用
 */
const handleSaleAdd = async (saleFormRef) => {
  if (!saleFormRef) return;
  await saleFormRef.validate(async (valid, fields) => {
    if (valid) {
      if (operateType.value === "edit") {
        projectForm.value.mainContactsInfoRespVoList[tableIndexSec.value] =
          JSON.parse(JSON.stringify(saleForm.value));
      } else {
        projectForm.value.mainContactsInfoRespVoList.push(
          JSON.parse(JSON.stringify({ ...saleForm.value }))
        );
      }
      saleForm.value = {
        brand: "",
        saleName: "",
        materialId: null,
        materialUnit: null,
        materialUnitName: "",
        platformName: "",
        technologyParameter: "",
        projectId: null,
        projectSaleId: null,
        quantity: null,
        remarks: "",
        saleCategory: null,
        saleCategoryName: "",
        saleName: "",
      };
      //   contactAddShow.value = false;
      saleFormRef.resetFields();
      nextTick(() => {
        refSelect.value.focus(); // 聚焦
      });
    } else {
      console.log("error submit!");
    }
  });
  //   operateType.value = "";
};

/**
 * @description: 处理销售清单操作
 */
const handleSaleOperate = () => {
  saleAddShow.value = true;
  operateType.value = "add";
  nextTick(() => {
    refSelect.value.focus(); // 聚焦
  });
};

/**
 * @description: 销售清单返回
 */
const handleSaleBack = () => {
  saleAddShow.value = false;
  nextTick(() => {
    refInput.value.focus(); // 聚焦
  });
};

/**
 * @description: 处理省市区操作
 * @param {String} type 类型
 */
const handleProvince = (type) => {
  switch (type) {
    case "province":
      cityList.value = [];
      areaList.value = [];
      projectForm.value.city = null;
      projectForm.value.area = null;
      getProvinceApi(projectForm.value.province).then((res) => {
        if (res.code === 200) {
          cityList.value = res.data;
        }
      });
      break;
    case "city":
      areaList.value = [];
      projectForm.value.area = null;
      getProvinceApi(projectForm.value.city).then((res) => {
        if (res.code === 200) {
          areaList.value = res.data;
        }
      });
      break;
  }
};

/**
 * @description: 处理项目选择
 */
const handleProjectChange = (value) => {
  if (value) {
    const project = projectList.value.find((item) => item.projectId === value);
    if (project) {
      projectForm.value.projectCode = project.projectCode;
    }
  } else {
    projectForm.value.projectCode = "";
  }
};

/**
 * @description: 处理销售清单表格操作
 * @param {String} type 操作类型
 * @param {Object} row  当前行数据
 * @param {Number} tabIndex 表格当前索引
 */
const handleOperateSec = (type, row, tabIndex) => {
  if (type === "edit") {
    tableIndexSec.value = tabIndex;
    saleForm.value = JSON.parse(JSON.stringify(row));
    saleAddShow.value = true;
    operateType.value = "edit";
    nextTick(() => {
      refSelect.value.focus(); // 聚焦
    });
  } else {
    projectForm.value.mainContactsInfoRespVoList.splice(tabIndex, 1);
  }
};

/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (id) => {
  try {
    detailInfoShow.value = true;
    const res = await getWorkApplyDetailApi(id);
    // res.mainOperationListRespVos = []

    projectForm.value = res;
    await getProvinceApi(projectForm.value.province).then((res) => {
      if (res.code === 200) {
        cityList.value = res.data;
      }
    });
    await getProvinceApi(projectForm.value.city).then((res) => {
      if (res.code === 200) {
        areaList.value = res.data;
      }
    });
    setTimeout(() => {
      detailInfoShow.value = false;
    }, 500);
  } catch (error) {
    detailInfoShow.value = false;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  // 项目列表
  await getProjectListApi().then((res) => {
    if (res) {
      projectList.value = res;
    }
  });
  // 工单类型
  await getDictsApi("erp_work_order_type").then((res) => {
    if (res.code === 200) {
      typeList.value = res.data;
    }
  });
  // 用户
  await getUserListApi().then((res) => {
    if (res.code === 200) {
      userList.value = res.rows;
    }
  });
  // 省
  await getProvinceApi(1).then((res) => {
    if (res.code === 200) {
      provinceList.value = res.data;
    }
  });
}

onMounted(() => {
  getDicts();
  nextTick(() => {
    refInput.value.focus(); // 聚焦
  });
});

defineExpose({
  handleDetailInfo,
});
</script>
<style lang="scss" scoped>
:deep(.el-collapse-item__header) {
  background-color: #f8faff;
}
.projectAdd {
  padding: 20px 20px 0 20px;
  width: 100%;
  height: 100%;
  :deep(.el-date-editor.el-input, .el-date-editor.el-input__wrapper) {
    width: 268px !important;
    height: 34px !important;
  }
  .project-info {
    padding: 20px 20px 5px 20px;
    background-color: #f0f0f0;
    margin: 10px 0;
  }
  .contact-info {
    padding-top: 20px;
  }
  :deep(.el-table) {
    th.el-table__cell {
      background: #f8f8f8 !important;
    }
  }
}
.search-input {
  width: 268px;
  height: 34px;
}
</style>
